package com.tech.platform.cms.logger.utils;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;

/**
 * @author shengqiang.wu
 * @date 2020/3/24/024 15:49
 * @desc
 */
@Slf4j
@Component
public class SpringEventUtils implements ApplicationContextAware{

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringEventUtils.applicationContext = applicationContext;
    }

    public static void publishEvent(ApplicationEvent event) {
        if (applicationContext == null) {
            log.error("[dy cms logger] logger utils,publishEvent has error: applicationContent is null");
            return;
        }
        try {
            applicationContext.publishEvent(event);
        } catch (Exception ex) {
            log.error("[dy cms logger] logger utils,publishEvent has error:{}",ex.getMessage(),ex);
        }
    }
}
